/**
 * 
 */
package com.fc.push.handler.pub.verify;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import org.springframework.util.Base64Utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fc.commons.HttpUtil;
import com.fc.commons.JsonUtils;
import com.fc.commons.security.Base64Util;
import com.fc.commons.security.MD5Util;

/**
 * @author Administrator
 *
 */
public class ApplicationUpdateHandlerBatch {
	String module = "1";
	String order = "5";

	JSONObject jsonParam;
	
	private static final String DEFAULT_ENCODE = "utf-8";

	public static final String MODULE = "module";
	public static final String ORDER = "order";
	
	private static final String PLATFORM = "platform";
	private static final String APPKEY = "appkey";
	private static final String APPNAME = "appname";
	private static final String PNAME = "pname";
	private static final String PKCSFILE = "pkcsfile";
	private static final String PWD = "pwd";
	private static final String IS_PRODUCTION = "isproduction";
	
	
	public ApplicationUpdateHandlerBatch(String appkey,String isProduction,String fileName){
		
		jsonParam = new JSONObject();
		jsonParam.put(PLATFORM,"ios");
		jsonParam.put(MODULE, module);
		jsonParam.put(ORDER, order);
		
		//jsonParam.put(APPKEY,"4c6cc04796434f8d9e6e3efe80311f3c");
		//jsonParam.put(APPKEY,"4d9b36ffde2746f9a38a08e2e1a1b1c5");
		
		
//		jsonParam.put(APPKEY,"4d9b36ffde2746f9a38a08e2e1a1b1c5");
//		jsonParam.put(IS_PRODUCTION,"1");
//		jsonParam.put(PLATFORM,"ios");
		
		jsonParam.put(APPKEY,appkey);
		jsonParam.put(IS_PRODUCTION,isProduction);
		jsonParam.put(PLATFORM,"ios");
		
		
		jsonParam.put(PWD,"123456");
		
		String filename = fileName;
				
		File file=new File(filename);//要识别的文件路径
		byte[] filedata = null;
		try {
			 filedata = file2byte(file);
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		jsonParam.put(PKCSFILE,filedata);
		
		
	}
	public void run(){
		try {
			
			System.out.println(jsonParam);
			
			
//			System.out.println(HttpUtil.doPost("http://ms.carisok.com/fcpserver/push", jsonParam));
//			System.out.println(HttpUtil.doPost("http://localhost:8080/fcpserver/push", jsonParam));
			System.out.println(HttpUtil.doPost("http://120.236.176.179:8088/fcpserver/push", jsonParam));
//			System.out.println(HttpUtil.doPost("http://120.236.176.179:8088/fcpserver/push", jsonParam));
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		
		//枫车汽配正式环境证书
		new ApplicationUpdateHandlerBatch("9e569d3e25b04259bfbab1a13bc9276d","1","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\imall_push_pro.p12").run();
		//枫车汽配开发环境证书
		new ApplicationUpdateHandlerBatch("3f158099e4904a0494d7d09ae1ab5911","0","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\imall_push_dev.p12").run();
		
		//门店正式
		new ApplicationUpdateHandlerBatch("8543aaa4b3384d0c817a437b8b9d2a37","1","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\sstore_push_pro.p12").run();
		//门店开发
		new ApplicationUpdateHandlerBatch("e4182d1f26b6464bb07ecd179b56c062","0","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\sstore_push_dev.p12").run();
		
		
		//快手正式
		new ApplicationUpdateHandlerBatch("c9a8d4b4c87c413ea385e03754f3082d","1","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\icar_push_pro.p12").run();
		//快手开发
		new ApplicationUpdateHandlerBatch("1122dbcedb954074aca4bae107c0073b","0","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\icar_push_dev.p12").run();

		
		//商家正式
		new ApplicationUpdateHandlerBatch("de25d601456b48ee871fed94d1e49b55","1","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\iboss_push_pro.p12").run();
		//商家开发
		new ApplicationUpdateHandlerBatch("d4fe3ed0d11f4a84999f900964f0422c","0","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\iboss_push_dev.p12").run();

		
		//师傅正式
		new ApplicationUpdateHandlerBatch("4d9b36ffde2746f9a38a08e2e1a1b1c5","1","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\expert_push_pro.p12").run();
		//师傅开发
		new ApplicationUpdateHandlerBatch("4c6cc04796434f8d9e6e3efe80311f3c","0","C:\\Users\\Administrator\\Desktop\\p12证书\\p12证书\\expert_push_dev.p12").run();

	}
	
	public static byte[] file2byte(File file) throws IOException {
		byte[] bytes = null;
		if (file != null) {
			InputStream is = new FileInputStream(file);
			int length = (int) file.length();
			if (length > Integer.MAX_VALUE) // 当文件的长度超过了int的最大值
			{
				System.out.println("this file is max ");
				return null;
			}
			bytes = new byte[length];
			int offset = 0;
			int numRead = 0;
			while (offset < bytes.length&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
				offset += numRead;
			}
			// 如果得到的字节长度和file实际的长度不一致就可能出错了
			if (offset < bytes.length) {
				System.out.println("file length is error");
				return null;
			}
			is.close();
		}
		return bytes;
	}
}
